﻿using AutoMapper;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SassModule.Contracts;
using SassModule.Entity;
using SassModule.ViewModel;
using System;

namespace SassModule.Controllers
{
    /// <summary>
    /// SassController
    /// </summary>
    [ApiController]
    [Route("Sass/[controller]")]
    public class SassController : ControllerBase
    {
        private readonly ILogger _logger;
        private readonly IWebHostEnvironment _env;
        private readonly ISassService _service;
        private readonly IMapper _mapper;
        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="env"></param>
        /// <param name="service"></param>
        public SassController(IMapper mapper,ILogger<SassController> logger, IWebHostEnvironment env, ISassService service)
        {
            _logger = logger;
            _env = env;
            _service = service;
            _mapper = mapper;
        }
        /// <summary>
        /// SASS列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index()
        {
            return Ok(new ResponseData(null,_service.Index()));
        }
        /// <summary>
        /// 新建
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(Sass sass)
        {
            _service.Add(sass);
            return Ok(new ResponseData(null,null));
        }
        /// <summary>
        /// 删除Id账套
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(Guid id)
        {
            _service.Del(id);
            return Ok(new ResponseData(null, null));
        }
        /// <summary>
        /// 账套下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet("Downlist")]
        public IActionResult Index4Select()
        {
            return Ok(new ResponseData(null, _service.DownList()));
        }
        /// <summary>
        /// 账套切换状态
        /// </summary>
        /// <param name="sass"></param>
        /// <returns></returns>
        [HttpPost("Switch")]
        public IActionResult Switch(SassViewModel sass)
        {
            _service.Switch(sass);
            return Ok(new ResponseData(null, null));
        }
        /// <summary>
        /// 重置账套密码
        /// </summary>
        /// <param name="sass"></param>
        /// <returns></returns>
        [HttpPost("Reset")]
        public IActionResult Reset(SassPasswordViewModel sass)
        {
            _service.Reset(sass);
            return Ok(new ResponseData(null, null));
        }
        /// <summary>
        /// 设置Title和logo
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("ConfigLogoAndTitle")]
        public IActionResult ConfigLogoAndTitleConfig([FromBody]SassConfigViewModel data)
        {
            string dbStr = Common.GetHeader4DB(this.Request);
            _service.ConfigLogoAndTitle(data,dbStr);
            return Ok(new ResponseData(null, null));
        }

        /// <summary>
        /// 更新sass
        /// </summary>
        /// <param name="sass"></param>
        /// <returns></returns>
        [HttpPut("update")]
        public IActionResult UpdateSass(Sass sass)
        {
            _service.UpdateSass(sass);
            return Ok(new ResponseData(null, null));
        }

    }
}
